use crate::subcmd::basecmd::BaseCmd;
use rcmd_core::clap::ArgMatches;
use rcmd_core::log::error;
use crate::global::{DpmError};
use async_trait::async_trait;
use crate::subcmd::{is_ymlconf, get_exec_path, lazy_init_ymlconf};


struct Init {
    path: String,
}

impl Init {
    fn new(path: &str) -> Self {
        Init {
            path: String::from(path)
        }
    }
}

#[async_trait]
impl BaseCmd for Init {
    async fn run(&self) -> Result<(), DpmError> {
        if is_ymlconf(&self.path) == true {
            return Err(DpmError::AlreadyInit);
        };
        let ret = lazy_init_ymlconf(self.path.as_str());
        ret
    }

}

pub async fn handle(_: &ArgMatches) {
    let p = get_exec_path();
    let cmd = &Init::new(p.as_str());
    cmd.run().await.unwrap_or_else(|e|{
        error!("{}",
        e.to_string().as_str());
    });
}

#[test]
fn test_init(){
    use std::env::temp_dir;
    let path_tmp = temp_dir();
    //chk init
    let path_str = path_tmp.display().to_string();
    
    is_ymlconf(path_str.as_str());
    let _ = lazy_init_ymlconf(path_str.as_str());
}